const { Article } = require("../model/article");
const { Like } = require("../model/like");
const jwt = require("jsonwebtoken");
/**
 * 获取文章列表
 */
exports.getArticles = async (req, res, next) => {
  try {
    // 设置单页返回文章数量
    const pageSize = 10;
    // 获取查询第几页
    const page = req.query.page ? Number.parseInt(req.query.page) : 1;
    // 输出参数
    console.log(page);
    // 查询所有文章
    // limit 限制查询多少条数据
    // offset 跳过多少条数据
    const allArticles = await Article.findAll({
      // limit: pageSize,
      // offset: (page - 1) * pageSize,
    });
    res.status(201).json(allArticles);
  } catch (error) {
    next(error);
  }
};

// 获取用户关注的作者文章列表
exports.getFeedArticles = async (req, res, next) => {
  try {
    // 处理请求
    res.send("get /articles/feed");
  } catch (error) {
    next(error);
  }
};

// 获取指定文章
exports.getArticle = async (req, res, next) => {
  try {
    // 查询文章 id为文章id
    const article = await Article.findOne({
      where: {
        id: req.params.id,
      },
    });
    // 文章浏览数加1
    article.watchSum += 1;
    // 保存文章信息
    await article.save();
    console.log(article)
    res.status(200).json(article);
  } catch (error) {
    next(error);
  }
};

/**
 * 创建文章
 */
exports.createArticle = async (req, res, next) => {
  try {
    // 保存文章信息
    console.log(req.body.article)
    const article = await Article.create(req.body.article);
    console.log(article)
    res.status(201).json(article);
  } catch (error) {
    next(error);
  }
};

/**
 * 更新文章
 */
exports.updateArticle = async (req, res, next) => {
  try {
    console.log(req.body.article);
    // 更新文章
    const article = await Article.update(req.body.article, {
      where: {
        id: req.params.id,
      },
    });
    res.status(201).json("ok");
  } catch (error) {
    next(error);
  }
};
/**
 * 管理员更新文章
 */
exports.masterUpdateArticle = async (req, res, next) => {
  try {
    const article = await Article.update(req.body.article, {
      where: {
        id: req.params.id,
      },
    });
    // 保存文章信息
    // await article.save();
    res.status(201).json("ok");
  } catch (error) {
    next(error);
  }
};
/**
 * 删除文章
 */
exports.deleteArticle = async (req, res, next) => {
  try {
    res.send("deleteArticle");
  } catch (error) {
    next(error);
  }
};

/**
 * 管理员删除文章
 */
exports.masterDeleteArticle = async (req, res, next) => {
  try {
    // 删除文章
    const article = await Article.destroy({
      where: {
        id: req.params.id,
      },
    });
    res.status(201).json("ok");
  } catch (error) {
    next(error);
  }
};

/**
 * 点赞文章
 */
exports.favoriteArticle = async (req, res, next) => {
  try {
    // 处理请求
    console.log(req.body.username);
    // 解析token获取用户名
    const username = jwt.decode(req.headers.authorization.split(" ")[1]).user
      .username;
    // 创建点赞记录
    const like = await Like.create({
      username: username,
      articleId: req.params.id,
    });
    // 增加文章点赞数
    const article = await Article.findOne({
      where: {
        id: req.params.id,
      },
    });
    article.likeSum += 1;
    await article.save();
    res.status(200).json("like");
  } catch (error) {
    next(error);
  }
};

/**
 * 取消文章
 */
exports.unfavoriteArticle = async (req, res, next) => {
  try {
    // 处理请求
    // 解析token获取用户名
    const username = jwt.decode(req.headers.authorization.split(" ")[1]).user
      .username;
    // 删除点赞记录
    const like = await Like.destroy({
      where: {
        username: username,
        articleId: req.params.id,
      },
    });
    // 减少文章点赞数
    const article = await Article.findOne({
      where: {
        id: req.params.id,
      },
    });
    article.likeSum -= 1;
    await article.save();
    res.status(200).json("unlike");
  } catch (error) {
    next(error);
  }
};

/**
 * 判断指定文章是否被当前用户点赞
 */
exports.isFavoriteArticle = async (req, res, next) => {
  try {
    // 解析token获取用户名
    const username = jwt.decode(req.headers.authorization.split(" ")[1]).user
      .username;
    // 处理请求
    const like = await Like.findOne({
      where: {
        username: username,
        articleId: req.params.id,
      },
    });
    if (like) {
      res.status(200).json({ favorited: true });
    } else {
      res.status(200).json({ favorited: false });
    }
  } catch (error) {
    next(error);
  }
};

/**
 * 添加文章评论
 */
exports.createArticleComment = async (req, res, next) => {
  try {
    // 处理请求
    res.send("post /articles/:slug/comments");
  } catch (error) {
    next(error);
  }
};

// 获取文章评论
exports.getArticleComments = async (req, res, next) => {
  try {
    // 处理请求
    res.send("get /articles/:slug/comments");
  } catch (error) {
    next(error);
  }
};

// 删除文章评论
exports.deleteArticleComment = async (req, res, next) => {
  try {
    // 处理请求
    res.send("delete /articles/:slug/comments/:id");
  } catch (error) {
    next(error);
  }
};
